<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增首页信息维护')" />
    <th:block th:include="include :: summernote-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-home-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">标题：</label>
                <div class="col-sm-8">
                    <input name="title" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">头部信息：</label>
                <div class="col-sm-8">
                    <input name="tips" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">尾部备注：</label>
                <div class="col-sm-8">
                    <input name="remark" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">排序：</label>
                <div class="col-sm-8">
                    <input name="sort" class="form-control" type="number" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">状态：</label>
                <div class="col-sm-8">
                    <label class="toggle-switch switch-solid">
                        <input type="checkbox" id="status" checked>
                        <span></span>
                    </label>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">内容：</label>
                <div class="col-sm-8">
                    <div class="summernote">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <script type="text/javascript">
        let prefix = ctx + "system/home";
        $("#form-home-add").validate({
            focusCleanup: true
        });

        $(document).ready(function () {
            $('.summernote').summernote({
                lang: 'zh-CN',
                callbacks: {
                    onImageUpload: function (files) {
                        let formData = new FormData();
                        formData.append('file', files[0]);
                        $.ajax({
                            url: ctx + "common/upload",
                            type: 'post',
                            cache: false,
                            data: formData,
                            processData: false,
                            contentType: false,
                            dataType: "json",
                            success: function (result) {
                                $('.note-editable').append('<img alt="' + result.fileName + '" src="' + result.url + '">');
                            }
                        });
                    }
                }
            });
        });

        function submitHandler() {
            if ($.validate.form()) {
                let data = $('#form-home-add').serializeArray();
                data.push({name: 'content', value: $('.summernote').summernote('code')});
                data.push({name: 'status', value: $("input[id='status']").is(':checked') ? 0 : 1})
                $.operate.save(prefix + "/add", data);
            }
        }
    </script>
</body>
</html>